package main

import (
	"bufio"
	"flag"
	"fmt"
	"go-barcode/internal/api"
	"go-barcode/internal/dispatch"
	"go-barcode/internal/imp/BarCode"
	"go-barcode/internal/imp/QrCode"
	"go-barcode/pkg/tools"
	"image/color"
	"os"
	"path/filepath"
)

var (
	configFile string
	qrTextPath string
	brTextPath string
	version    bool
	cfg        bool
)

func init() {
	defaultConfig, _ := filepath.Abs(fmt.Sprintf(".%sconfig%sconfig.yml", string(filepath.Separator), string(filepath.Separator)))
	qrtmpPath, _ := filepath.Abs(fmt.Sprintf(".%sqrcode.txt", string(filepath.Separator)))
	flag.StringVar(&qrTextPath, "qrpath", qrtmpPath, "二维码文本文件路径")
	brtmpPath, _ := filepath.Abs(fmt.Sprintf(".%sbrcode.txt", string(filepath.Separator)))
	flag.StringVar(&brTextPath, "brpath", brtmpPath, "条形码文本文件路径")

	flag.StringVar(&configFile, "config", defaultConfig, "配置文件地址")
	flag.BoolVar(&version, "version", false, "版本")
	flag.BoolVar(&cfg, "cfg", cfg, "创建默认配置文件")
}

func main() {
	flag.Parse()
	getwd, _ := os.Getwd()
	inputReader := bufio.NewReader(os.Stdin)
	qrPath := fmt.Sprintf("%s%sqrimage%s", getwd, string(filepath.Separator), string(filepath.Separator))
	brPath := fmt.Sprintf("%s%sbrimage%s", getwd, string(filepath.Separator), string(filepath.Separator))
	err := checkAndCreatePath(brPath)
	if err != nil {
		fmt.Printf("%v\n", err)
		_, _, _ = inputReader.ReadLine()
		return
	}
	err = checkAndCreatePath(qrPath)
	if err != nil {
		fmt.Printf("%v\n", err)
		_, _, _ = inputReader.ReadLine()
		return
	}
	c, err := tools.ReadConfigFile(configFile)
	w := 256
	h := 128
	if err != nil {
		fmt.Printf("配置文件读取错误,错误信息:%v\n", err)
	} else {
		w = c["size.w"].(int)
		h = c["size.h"].(int)
	}
	img := api.ImageCode{
		Size: api.ImageSize{
			Height: h,
			Width:  w,
		},
		Color: api.ImageColor{
			Background: color.White,
			Foreground: color.Black,
		},
	}
	createQrcode(qrPath, img, inputReader)

	createBarCode(brPath, img, inputReader)

	println("生成完毕,请按回车键退出....")
	_, _, _ = inputReader.ReadLine()
}

func createQrcode(qrPath string, img api.ImageCode, inputReader *bufio.Reader) bool {
	if tools.FileExit(qrTextPath) {
		d := new(dispatch.Dispatcher)
		d.Creator = new(QrCode.QrCode)

		err := d.Create(qrTextPath, qrPath, img)
		if err != nil {
			sprintf := fmt.Sprintf("生成二维码失败,错误信息:%v", err)
			println(sprintf)
			_, _, _ = inputReader.ReadLine()
			return true
		}
		println("二维码生成完毕!")
	}
	return false
}

func createBarCode(brPath string, img api.ImageCode, inputReader *bufio.Reader) bool {
	if tools.FileExit(brTextPath) {
		d := new(dispatch.Dispatcher)
		d.Creator = new(BarCode.BarCode)
		err := d.Create(brTextPath, brPath, img)
		if err != nil {
			sprintf := fmt.Sprintf("生成条形码失败,错误信息:%v", err)
			println(sprintf)
			_, _, _ = inputReader.ReadLine()
			return true
		}
		println("条码生成完毕!")
	}
	return false
}

func checkAndCreatePath(path string) error {
	exists := tools.PathExists(path)
	if !exists {
		err := os.MkdirAll(path, os.ModePerm)
		if err != nil {
			info := fmt.Errorf("创建目录[%v]失败,错误信息:%v", path, err)
			return info
		}
	}
	return nil
}
